//
//  SPBaseCollectionViewCell.m
//  SmartCity
//
//  Created by LSP on 2017/5/19.
//  Copyright © 2017年 NRH. All rights reserved.
//

#import "SPBaseCollectionViewCell.h"
#import "SPBaseViewController.h"

@implementation SPBaseCollectionViewCell {
    UIView *_highlightedView;
}
- (void)dealloc {
    [self removeObserver:self forKeyPath:@"reuseIdentifier" context:nil];
}

- (instancetype)initWithCoder:(NSCoder *)coder
{
    self = [super initWithCoder:coder];
    if (self) {
        [self addObserver:self forKeyPath:@"reuseIdentifier" options:(NSKeyValueObservingOptionNew) context:nil];
    }
    return self;
}

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        _highlightEnabled = YES;
        
        [self setupSubviews];
        
        [self addObserver:self forKeyPath:@"reuseIdentifier" options:(NSKeyValueObservingOptionNew) context:nil];
    }
    return self;
}

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
    _highlightEnabled = YES;
    
    [self setupSubviews];
}

- (void)setupSubviews {
#if DEBUG
    NSLog(@"%@:子类 -> %@ 重载方法错误. %@", @"SPBaseCollectionViewCell", NSStringFromClass([self class]), @"***子类需重载此方法，且无需调用 [super setupSubviews]。***");
#endif
}

- (void)configForReuseIdentifier:(NSString *)reuseIdentifier {
    // 供子类重载
}

- (BOOL)ensureHadLoginIfFailed:(void(^)(void))failureContinue {
//    if ([[SingleClass userId] isValidString]) {
//        return YES;
//    }
//    UIViewController *vc = self.nextResponder;
//    while (![vc isKindOfClass:[UIViewController class]] && vc.nextResponder) {
//        vc = vc.nextResponder;
//    }
//    if ([vc isKindOfClass:[SPBaseViewController class]]) {
//        [(SPBaseViewController *)vc gotoLoginAndSuccessCallback:failureContinue];
//    }else if ([vc isKindOfClass:[UIViewController class]]) {
//        LoginVC *v = [LoginVC new];
//        v.loginComplete = failureContinue;
//        
//        UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:v];
//        [vc presentViewController:nav animated:YES completion:nil];
//    }
    return NO;
}

- (void)setSelected:(BOOL)selected {
    [self setHighlighted:selected];
}

- (void)setHighlighted:(BOOL)highlighted {
    if (!_highlightEnabled) {
        return;
    }
    UIView *view = self.highlightedView;
    
    if (highlighted) {
        view.alpha = .01;
        [self.contentView addSubview:view];
    }
    [UIView animateWithDuration:.15 delay:0 options:6
                     animations:^{
                         view.alpha = highlighted ? 1.0 : .01;
                     } completion:^(BOOL finished) {
                         if (!highlighted) {
                             view.alpha = 1.;
                             [view removeFromSuperview];
                         }
                     }];

}

- (UIView *)highlightedView {
    if (!_highlightedView) {
        _highlightedView = [UIView new];
        _highlightedView.frame = self.bounds;
        _highlightedView.userInteractionEnabled = NO;
        _highlightedView.backgroundColor = [UIColor colorWithWhite:0.6 alpha:.3];
    }
    return _highlightedView;
}

- (void)setBounds:(CGRect)bounds {
    [super setBounds:bounds];
    _highlightedView.frame = bounds;
}

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSKeyValueChangeKey,id> *)change context:(void *)context {
    if (![keyPath isEqualToString:@"reuseIdentifier"])
        return;
    
    if (!self.reuseIdentifier)
        return;
    
    [self configForReuseIdentifier:self.reuseIdentifier];
}

@end
